package com.ruoyi.jksd.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.jksd.common.dto.ConveyDto;
import com.ruoyi.jksd.common.dto.XySdDto;
import com.ruoyi.jksd.domain.XyUsers;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.jksd.domain.XyConvey;
import com.ruoyi.jksd.service.IXyConveyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 会员-订单Controller
 *
 * @author ruoyi
 * @date 2024-05-26
 */
@RestController
@RequestMapping("/convey/convey")
public class XyConveyController extends BaseController {
    @Autowired
    private IXyConveyService xyConveyService;

    /**
     * 查询会员-订单列表
     */
    @PreAuthorize("@ss.hasPermi('convey:convey:list')")
    @GetMapping("/list")
    public TableDataInfo list(XyConvey xyConvey) {
        startPage();
        List<XyConvey> list = xyConveyService.selectXyConveyList(xyConvey);
        return getDataTable(list);
    }

    /**
     * 导出会员-订单列表
     */
    @PreAuthorize("@ss.hasPermi('convey:convey:export')")
    @Log(title = "会员-订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, XyConvey xyConvey) {
        List<XyConvey> list = xyConveyService.selectXyConveyList(xyConvey);
        ExcelUtil<XyConvey> util = new ExcelUtil<XyConvey>(XyConvey.class);
        util.exportExcel(response, list, "会员-订单数据");
    }

    /**
     * 获取会员-订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('convey:convey:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(xyConveyService.selectXyConveyById(id));
    }

    /**
     * 新增会员-订单
     */
    @PreAuthorize("@ss.hasPermi('convey:convey:add')")
    @Log(title = "会员-订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody XyConvey xyConvey) {
        return toAjax(xyConveyService.insertXyConvey(xyConvey));
    }

    /**
     * 修改会员-订单
     */
    @PreAuthorize("@ss.hasPermi('convey:convey:edit')")
    @Log(title = "会员-订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody XyConvey xyConvey) {
        return toAjax(xyConveyService.updateXyConvey(xyConvey));
    }

    /**
     * 删除会员-订单
     */
    @PreAuthorize("@ss.hasPermi('convey:convey:remove')")
    @Log(title = "会员-订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(xyConveyService.deleteXyConveyByIds(ids));
    }


    /**
     * 新增会员-订单
     */
    @Log(title = "会员-订单", businessType = BusinessType.INSERT)
    @PostMapping("/applyOrderConvey")
    public AjaxResult applyOrderConvey() {
        return xyConveyService.applyOrderConvey(getLoginUser());
    }

    /**
     * 用户提交订单，扣除余额，记录订单状态
     *
     * @param conveyDto
     * @return
     */
    @Log(title = "会员-订单", businessType = BusinessType.UPDATE)
    @PostMapping("doApplyOrder")
    public AjaxResult doApplyOrder(@RequestBody ConveyDto conveyDto) {

        return xyConveyService.doAapplyOrder(conveyDto);

    }
}
